草庐IT

Golang 的 Int flag 和 time.After

全部标签

function - Golang 文档中 "a"之前的 "..."和 "interface{}"是什么意思?

这个问题在这里已经有了答案:Meaningof...interface{}(dotdotdotinterface)(2个答案)关闭4年前。在官方文档中,经常出现如下代码。funcPrintf(formatstring,a...interface{})(nint,errerror)a和...分别是什么意思?

testing - 我应该使用哪种方法来测试 golang 中的 func main()?

我在main.go中有一个函数main()可以完成这项工作,所有其他函数都在它下面(我没有在这里包括它们)。因此,当我为main中包含的所有funcs编写测试时,我可以测试它们。但是代码覆盖率很低,因为它表明我没有覆盖main函数中的代码。我知道测试库中有一个TestMain函数可以完成这项工作,但我就是不知道如何让它工作,以便测试涵盖funcmain()。下面是我的main()函数,它没有被测试覆盖...funcmain(){c,err:=getConfig()iferr!=nil{log.Fatal(err)}slideshows,err:=getSlideshows(c)ifer

Golang 错误 "invalid memory address or nil pointer dereference",为什么会这样?

这个问题在这里已经有了答案:Go:panic:runtimeerror:invalidmemoryaddressornilpointerdereference(6个答案)关闭4年前。packagemainimport("fmt""net/http")funcmain(){http.HandleFunc("/",handler)http.HandleFunc("/cookie",cookie)http.ListenAndServe(":8080",nil)}funchandler(whttp.ResponseWriter,r*http.Request){//do...}funccooki

debugging - 如何分析这个 Golang cpu pprof 快照?

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭3年前。Improvethisquestion此问题发生在程序启动后约10分钟。CPU成本300%。有什么问题?stackoverflow不支持.svg图像。请下载上传到github的.svg文件。https://github.com/.../raw/master/pprof001.svg

go - 达到 time.Now() 时进行循环

在Golang中是否可以通过给定的日期变量增加for循环中的日期,直到达到当前日期/时间。Now()//Startdatet,_:=time.Parse(time.RFC3339,"2018-07-19T12:25:10.8584224+02:00")//Currentdatect:=time.Now()ford:=t;d.Day()==ct.Day();d=d.AddDate(0,0,1){//Printalldaysbetweenstartdateandcurrentdatefmt.Println(d)}我希望变量d打印出所有日期(包括时间等),直到它到达当前日期

go - Golang内置函数 "make"如何实现不同类型的参数长度检查?

在文档中,API显示make接受类型和可变大小的IntegerType参数。funcmake(tType,size...IntegerType)Type做一个数组,我可以传3个参数,比如制作([]int,3,5)但是当我尝试制作map时make(map[int]int,3,5)当我编译时,它弹出toomanyargumentstomake(map[int]int)。这与编译器有关吗?是否可以为我自己的函数实现此行为? 最佳答案 编译器对make和其他内置函数有特殊的了解。编译器对make正在初始化的值类型强制执行允许的参数计数。编译

dictionary - 如何从使用接口(interface){}键入的 golang map[string] 字符串中提取值

这个问题在这里已经有了答案:ExplainTypeAssertionsinGo(3个答案)AccessingNestedMapofTypemap[string]interface{}inGolang(2个答案)Typed,nestedmapofmap[string]interface{}returns"typeinterface{}doesnotsupportindexing"(1个回答)getvalueformnestedmaptypemap[string]interface{}(2个答案)howtoaccessnestedJsonkeyvaluesinGolang(3个答案)关闭3

golang 如何使用工具找到 ticker 泄漏的位置?

我发现我的进程在没有业务请求时使用了高CPU我使用go-torch发现大部分cpu都浪费在了runtime.timeproc我认为这一定是因为time.NewTicker在某处泄漏(而不是停止),或者在for循环中创建代码那么我怎样才能使用任何工具找到它事实上,我已经搜索过它并且所有代码都遵循deferticker.Stop() 最佳答案 我找到了找出泄漏Ticker的方法在堆配置文件中,您可以键入:gotoolpprofhttp://xxx/debug/pprof/heaptreetime.NewTicker它会显示代码创建的位置

golang 全局变量访问在基准测试中很慢

这是一个简单的golang基准测试,它以三种不同的方式运行x++:在一个简单的for循环中,在函数内部声明了x在函数内部声明了x的嵌套循环中在x声明为全局变量的嵌套循环中packagemainimport("testing")varx=0funcBenchmarkLoop(b*testing.B){x:=0forn:=0;n结果如下:$gotest-bench=.BenchmarkLoop-820000000000.32ns/opBenchmarkDoubleLoop-820000000000.34ns/opBenchmarkDoubleLoopGlobalVariable-82000

json - 如何在golang中打印json值

关闭。这个问题需要detailsorclarity。它目前不接受答案。想改进这个问题吗?添加细节并通过editingthispost澄清问题。关闭3年前。Improvethisquestion我有一些json并且想打印值,但我不知道如何从这样的json格式打印"order_items":[{"total":1,"unitprice":1,"price":1,"create_date":"2019-06-0713:51:36","flow_no":"1234","code":"4567","quantiry":1,"discount_ctotal":0,"img":"","fname":